package vn.student.mylib;

import java.util.ArrayList;
import vn.student.R;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ListViewAdapter extends ArrayAdapter<Contact> {
	Activity context;
	ArrayList<Contact> list;
	public ListViewAdapter(Activity context,ArrayList<Contact> arrayList) {
		super(context, R.layout.contact_row,arrayList);
		this.context=context;
		this.list = arrayList;
	}
	public View getView(int position, View convertView,ViewGroup parent) {
		 View row=convertView;
		 RowWrapper wrapper=null;
	      
	     if (row==null) {                         
	    	 LayoutInflater inflater=context.getLayoutInflater();
	        
	    	 row=inflater.inflate(R.layout.contact_row, null);	    		 
	    	 wrapper=new RowWrapper(row);
	    	 row.setTag(wrapper);
	     }
	     else {
	    	 wrapper=(RowWrapper)row.getTag();
	     }
	     wrapper.getNameLabel().setText(list.get(position).getName());
	     wrapper.getEmailLabel().setText(list.get(position).getEmail());
	     wrapper.getPhoneNoLabel().setText(list.get(position).getPhoneNo());
	     wrapper.getIcon().setImageResource(R.drawable.icon);
	     /*
	     
	     if(list.size()==1){
    		 row.setBackgroundResource(R.drawable.class_row_all);
    	 }
    	 else{
    		 if(position==(list.size()-1))
    			 row.setBackgroundResource(R.drawable.class_row_bottom);
    		 else 
    			 if(position==0)
    				 row.setBackgroundResource(R.drawable.class_row_top);
    			 else
    				 row.setBackgroundResource(R.drawable.class_row_none);
    	 }
    	 
    	 */
	     return(row);
	}
	public ArrayList<Contact> getContactInfoList(){
		return list;
	}
	public void setContactInfoList(ArrayList<Contact> list){
		this.list = list;
	}
	private class RowWrapper {
		View base;
		TextView id = null;
		TextView name=null;
		TextView phoneNo=null;
		TextView email=null;
		ImageView icon=null;
	  
		RowWrapper(View base) {
			this.base=base;
		}
		/*
		TextView getIdLabel() {
			if (id==null) {
				id=(TextView)base.findViewById(R.id.idInvi);
			}
		  return(id);
		}
		*/
		TextView getNameLabel() {
			if (name==null) {
				name=(TextView)base.findViewById(R.id.name);
			}
		  return(name);
		}
		TextView getEmailLabel() {
			if (phoneNo==null) {
				phoneNo=(TextView)base.findViewById(R.id.email);
			}
		  return(phoneNo);
		}
		TextView getPhoneNoLabel() {
			if (email==null) {
				email=(TextView)base.findViewById(R.id.phoneNo);
			}
		  return(email);
		}

		ImageView getIcon() {
			if (icon==null) {
				icon=(ImageView)base.findViewById(R.id.icon);
			}
			return(icon);
		}
	}
}
